package com.lzy.cloud.member.controller;

import com.lzy.cloud.member.dtos.MemberUserDto;
import com.lzy.cloud.member.facade.MemberUserFacade;
import com.lzy.cloud.member.request.MemberUserRequest;
import com.lzy.cloud.member.response.MemberUserResponse;
import com.lzy.cloud.member.service.MemberUserService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.core.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by WIN7 on 2018/5/30.
 */
@RestController
@RequestMapping("/member")
public class MemberUserController {

    @Autowired
    MemberUserService memberUserService;

    @Autowired
    MemberUserFacade memberUserFacade;

    /**
     * @Title: 用户注册
     * @param: @return
     * @return: Result
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result registerMember(MemberUserRequest memberUserRequest) {
        Ensure.that(memberUserRequest).isNotNull("00010001");
        MemberUserDto dto = memberUserFacade.registerMember(DozerUtils.map(memberUserRequest, MemberUserDto.class));
        Ensure.that(dto).isNotNull("00020002");
        return new Result(dto.getId());
    }

    /**
     * 用户登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/validate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Result loginValidate(MemberUserRequest request){
        Ensure.that(request).isNotNull("00010001");
        MemberUserDto dto = memberUserFacade.loginValidate(DozerUtils.map(request,MemberUserDto.class));
        Ensure.that(dto).isNotNull("00020009");
        MemberUserResponse memberUserResponse = this.getMemberBaseResponseByParam(dto);
        return new Result(memberUserResponse);
    }

    /**
     * 更新会员
     * @param memberUserRequest
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result updateMember(MemberUserRequest memberUserRequest){
        Ensure.that(memberUserRequest).isNotNull("00010001");
        MemberUserDto dto = DozerUtils.map(memberUserRequest,MemberUserDto.class);
        memberUserFacade.updateMemberUser(dto);
        MemberUserResponse memberUserResponse = this.getMemberBaseResponseByParam(dto);
        return new Result(memberUserResponse);
    }

    /**
     * 根据会员id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public Result detail(@PathVariable("id") Long id){
        Ensure.that(id).isNotNull("00010001");
        MemberUserDto memberUserDto = memberUserService.selectByMemberId(id);
        Ensure.that(memberUserDto).isNotNull("00020007");
        return new Result(DozerUtils.map(memberUserDto, MemberUserResponse.class));
    }

    /**
     * 根据会员id删除会员（假删）
     * @param id
     * @return
     */
    @RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
    public Result delMemberId(@PathVariable("id") Long id){
        Ensure.that(id).isNotNull("00010001");
        MemberUserDto memberUserDto = new MemberUserDto();
        memberUserDto.setId(id);
        memberUserDto.setDeleted(Boolean.TRUE);
        Boolean ok = memberUserService.updateMemberUser(memberUserDto);
        Ensure.that(ok).isTrue("00020008");
        return new Result(ok);
    }

    /**
     * 根据条件查询会员列表
     * @param memberUserRequest
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Result selectList(MemberUserRequest memberUserRequest){
        Ensure.that(memberUserRequest).isNotNull("00010001");
        List<MemberUserDto> memberUserDtoList = memberUserService.selectList(DozerUtils.map(memberUserRequest,MemberUserDto.class));
        Ensure.that(memberUserDtoList).isNotNull("00020007");
        return new Result(DozerUtils.maps(memberUserDtoList, MemberUserResponse.class));
    }


    /**
     * 以下四种情况下，统一返回个人信息对象：
     * 0、登录功能
     * 1、修改个人信息
     * 2、修改登录密码
     * 3、修改支付密码
     * 4、绑定手机号
     * 5、实名认证（待...）
     *
     * @param dto
     * @return
     */
    private MemberUserResponse getMemberBaseResponseByParam(MemberUserDto dto) {
        MemberUserResponse response = new MemberUserResponse();
        response.setId(dto.getId());
        response.setIdNumber(dto.getIdNumber());
        response.setPhone(dto.getPhone());
        response.setSex(dto.getSex());
        String nickName = dto.getNickname();
        if (nickName == null || "".equals(nickName)) {
            nickName = "LZY_NO_" + dto.getPhone();
        }
        if (StringUtil.isBlank(dto.getPayPassword())) {
            response.setPayment(0);
        } else {
            response.setPayment(1);
        }
        response.setNickname(nickName);
        if (dto.getName() == null || "".equals(dto.getName())) {
            response.setName(nickName);
        } else {
            response.setName(dto.getName());
        }
        response.setAvater(dto.getAvater());
        response.setSex(dto.getSex());
        response.setBirthday(dto.getBirthday());
        response.setAddress(dto.getAddress());
//        if (memberUserDto != null) {
//            response.setShopId(memberUserDto.getShopId());
//            memberUserDto.setShopId(memberUserDto.getShopId());
//        }
        //3.用户身份信息及
//        List<Long> identityList = sysUserIdentityServiceApi.selectByUserPhone(memberUserDto.getAccount());
//        if (identityList != null && identityList.size() > 0) {
//            response.setIdentityIdList(identityList);
//            memberUserDto.setIdentityList(identityList);
//        }
//        //4.token
//        if (token == null || "".equals(token)) {
//            token = this.createAndSaveToken(memberUserDto);
//        }
        response.setToken(memberUserFacade.createAndSaveToken(dto));
        //5.获取用户的地址条数
//        Integer count = userAddressApi.hasUserAddress(memberUserDto.getId());
//        if (count == null) {
//            count = 0;
//        }
//        kvClient.save(OrderDistributePrefix.USER_ADDRESS_COUNT + memberUserDto.getId(),
//                count, Integer.MAX_VALUE);
        return response;
    }
}
